package com.photographysharing.controller;


import com.photographysharing.common.ThreadLocalOperation;
import com.photographysharing.dto.PhotoDTO;
import com.photographysharing.entity.Photos;
import com.photographysharing.entity.Statistics;
import com.photographysharing.exception.ServiceException;
import com.photographysharing.request.SelectByPaging;
import com.photographysharing.response.DataResponse;
import com.photographysharing.response.DatasResponse;
import com.photographysharing.response.MessageResponse;
import com.photographysharing.service.PhotosService;
import com.photographysharing.service.StatisticsService;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/photo")
@RestController
public class PhotoController {

    private PhotosService photosService;
    private StatisticsService statisticsService;

    public PhotoController(PhotosService photosService, StatisticsService statisticsService) {
        this.statisticsService = statisticsService;
        this.photosService = photosService;
    }

    @PostMapping("/add")
    public MessageResponse add(@RequestBody Photos photo){
        photo.setUserId(ThreadLocalOperation.getThreadLocalUserId());
        try{
            if(photosService.save(photo)){
                statisticsService.insertstatistics(photo.getId());
                return new MessageResponse("新增成功!");
            }else{
                return new MessageResponse("新增失败!");
            }
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }

    @PostMapping("/update")
    public MessageResponse update(@RequestBody Photos photo){
        try{
            if(photosService.saveOrUpdate(photo)){
                return new MessageResponse("更新成功!");
            }else{
                return new MessageResponse("更新失败!");
            }
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    public MessageResponse delete(String id){
        try{
            if(photosService.removeById(id)){
                return new MessageResponse("删除成功!");
            }else{
                return new MessageResponse("删除失败!");
            }
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }

    @GetMapping("/selectByVague")
    public DatasResponse<PhotoDTO> selectByVague(SelectByPaging pag){
        DatasResponse<PhotoDTO> res = photosService.select(pag);
        //顺便增加统计数据
        for(PhotoDTO data:res.getDatas()){
            Statistics statistics = statisticsService.getstatistics(data.getId());
            //给实体的count赋值
            data.setCount(statistics.getLikeCount(),statistics.getWatchCount(),statistics.getStarCount());
        }
        return res;
    }
    @GetMapping("/selectByPag")
    public DatasResponse<PhotoDTO> selectByPag(SelectByPaging pag){
        DatasResponse<PhotoDTO> res = photosService.selectByPag(pag);
        //顺便增加统计数据
        for(PhotoDTO data:res.getDatas()){
            Statistics statistics = statisticsService.getstatistics(data.getId());
            //给实体的count赋值
            data.setCount(statistics.getLikeCount(),statistics.getWatchCount(),statistics.getStarCount());
        }
        return res;
    }

    @GetMapping("/getById")
    public DataResponse<PhotoDTO> getById(String photoId){
        PhotoDTO data = photosService.getById(photoId);
        try{
            Statistics statistics = statisticsService.getstatistics(data.getId());
            data.setCount(statistics.getLikeCount(),statistics.getWatchCount(),statistics.getStarCount());
            return new DataResponse<>(data);
        }catch (Exception e){
            var a = e;
            throw new ServiceException("");
        }


    }
    @GetMapping("/selectByUserId")
    public DatasResponse<PhotoDTO> selectByUserId(SelectByPaging pag){
        DatasResponse<PhotoDTO> res = photosService.selectByUserId(pag);
        if(res.isFail()){
            return res;
        }
        for(PhotoDTO data:res.getDatas()){
            Statistics statistics = statisticsService.getstatistics(data.getId());
            //给实体的count赋值
            data.setCount(statistics.getLikeCount(),statistics.getWatchCount(),statistics.getStarCount());
        }
        return res;
    }

}
